import { DeviceDevice, Ads, DeviceInfo, Device } from './../models/device';
import { Employee } from './../models/Employee';
import { employeesRestApi, EmployeesRestApi } from './../common/http/rest-api';
import { businsessHttpServer, cloundHttpServer } from './../common/http/http-server';
import { Observable, Subject, BehaviorSubject } from 'rxjs';



class DevcieFactory {
    adsAbout = { url: "1//2", ads_id: "2" }//debug
    generateDDSDevice() {
        const url = this.adsAbout.url
        const ip = url.split("//")[1].split(":")[0];
        const ads_id = this.adsAbout.ads_id;

        const deviceInfo: DeviceInfo = {
            "address": ip,
            "port": 9100,
            "extDeviceType": "kds-printer",
            "instructionSet": "kds/mpj"
        }
        const deviceDevice: DeviceDevice = {
            "type": "wifi",
            "address": ip, //本地地址
            "productName": ip,
            "manufacturer": ip,
            "deviceInfo": deviceInfo,
            "connectionType": "wifi"
        }
        const ads: Ads = {
            "retCode": 0,
            "version": "2.0",
            ads_id,
            "name": "kds-1",
            url, //本地地址
            "adapterIds": [
                ads_id
            ],
            "adsId": ads_id
        }
        const device: Device = {
            id: "",
            usage: "kds",
            instructionSet: "kds/mpj",
            ads,
            device: deviceDevice,
            deviceInfo,
            storeId: "123",
            isShared: true,
            connnectType: "wifi",
            deviceType: "kds-printer",
            name: "deviceName",
            deviceName: ip,
            state: "online"
        }

    }
}

class DevciesService {
    constructor(private businsessHttpServer, private cloundHttpServer) {
    }

    private httpResultCallback(result) {
        return new Promise((resolve, reject) => {
            result.then(response => {
                const rData = response.data;
                //老的java接口会这样子返回
                if (typeof rData.code == "undefined") {
                    resolve(rData);
                }
                if (rData.code == 200) resolve(rData.data);
                else reject(rData.message);
            }).catch(error => {
                console.log(error);
                reject(error);
            })
        });
    }

    getKDSDevice() {
        return this.httpResultCallback(businsessHttpServer("/api/devices?deviceType=kds-printer").get())
    }

    bindDDSDevice() {
        return this.httpResultCallback(businsessHttpServer("/api/devices?deviceType=kds-printer").get())
    }
}

export const devciesService = new DevciesService(businsessHttpServer, cloundHttpServer);